home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Include / objimpl.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  8.4 KB  |  210 lines

  1. #ifndef Py_OBJIMPL_H
  2. #define Py_OBJIMPL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7. #include "mymalloc.h"
  8.  
  9. /*
  10. Functions and macros for modules that implement new object types.
  11. You must first include "object.h".
  12.  
  13.  - PyObject_New(type, typeobj) allocates memory for a new object of
  14.    the given type; here 'type' must be the C structure type used to
  15.    represent the object and 'typeobj' the address of the corresponding
  16.    type object.  Reference count and type pointer are filled in; the
  17.    rest of the bytes of the object are *undefined*!  The resulting
  18.    expression type is 'type *'.  The size of the object is actually
  19.    determined by the tp_basicsize field of the type object.
  20.  
  21.  - PyObject_NewVar(type, typeobj, n) is similar but allocates a
  22.    variable-size object with n extra items.  The size is computed as
  23.    tp_basicsize plus n * tp_itemsize.  This fills in the ob_size field
  24.    as well.
  25.  
  26.  - PyObject_Del(op) releases the memory allocated for an object.
  27.  
  28.  - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
  29.    similar to PyObject_{New, NewVar} except that they don't allocate
  30.    the memory needed for an object. Instead of the 'type' parameter,
  31.    they accept the pointer of a new object (allocated by an arbitrary
  32.    allocator) and initialize its object header fields.
  33.  
  34. Note that objects created with PyObject_{New, NewVar} are allocated
  35. within the Python heap by an object allocator, the latter being
  36. implemented (by default) on top of the Python raw memory
  37. allocator. This ensures that Python keeps control on the user's
  38. objects regarding their memory management; for instance, they may be
  39. subject to automatic garbage collection.
  40.  
  41. In case a specific form of memory management is needed, implying that
  42. the objects would not reside in the Python heap (for example standard
  43. malloc heap(s) are mandatory, use of shared memory, C++ local storage
  44. or operator new), you must first allocate the object with your custom
  45. allocator, then pass its pointer to PyObject_{Init, InitVar} for
  46. filling in its Python-specific fields: reference count, type pointer,
  47. possibly others. You should be aware that Python has very limited
  48. control over these objects because they don't cooperate with the
  49. Python memory manager. Such objects may not be eligible for automatic
  50. garbage collection and you have to make sure that they are released
  51. accordingly whenever their destructor gets called (cf. the specific
  52. form of memory management you're using).
  53.  
  54. Unless you have specific memory management requirements, it is
  55. recommended to use PyObject_{New, NewVar, Del}. */
  56.  
  57. /* 
  58.  * Core object memory allocator
  59.  * ============================
  60.  */
  61.  
  62. /* The purpose of the object allocator is to make make the distinction
  63.    between "object memory" and the rest within the Python heap.
  64.    
  65.    Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
  66.    the one that holds the object's representation defined by its C
  67.    type structure, *excluding* any object-specific memory buffers that
  68.    might be referenced by the structure (for type structures that have
  69.    pointer fields).  By default, the object memory allocator is
  70.    implemented on top of the raw memory allocator.
  71.  
  72.    The PyCore_* macros can be defined to make the interpreter use a
  73.    custom object memory allocator. They are reserved for internal
  74.    memory management purposes exclusively. Both the core and extension
  75.    modules should use the PyObject_* API. */
  76.  
  77. #ifndef PyCore_OBJECT_MALLOC_FUNC
  78. #undef PyCore_OBJECT_REALLOC_FUNC
  79. #undef PyCore_OBJECT_FREE_FUNC
  80. #define PyCore_OBJECT_MALLOC_FUNC    PyCore_MALLOC_FUNC
  81. #define PyCore_OBJECT_REALLOC_FUNC   PyCore_REALLOC_FUNC
  82. #define PyCore_OBJECT_FREE_FUNC      PyCore_FREE_FUNC
  83. #endif
  84.  
  85. #ifndef PyCore_OBJECT_MALLOC_PROTO
  86. #undef PyCore_OBJECT_REALLOC_PROTO
  87. #undef PyCore_OBJECT_FREE_PROTO
  88. #define PyCore_OBJECT_MALLOC_PROTO   PyCore_MALLOC_PROTO
  89. #define PyCore_OBJECT_REALLOC_PROTO  PyCore_REALLOC_PROTO
  90. #define PyCore_OBJECT_FREE_PROTO     PyCore_FREE_PROTO
  91. #endif
  92.  
  93. #ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
  94. extern ANY *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
  95. extern ANY *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
  96. extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
  97. #endif
  98.  
  99. #ifndef PyCore_OBJECT_MALLOC
  100. #undef PyCore_OBJECT_REALLOC
  101. #undef PyCore_OBJECT_FREE
  102. #define PyCore_OBJECT_MALLOC(n)      PyCore_OBJECT_MALLOC_FUNC(n)
  103. #define PyCore_OBJECT_REALLOC(p, n)  PyCore_OBJECT_REALLOC_FUNC((p), (n))
  104. #define PyCore_OBJECT_FREE(p)        PyCore_OBJECT_FREE_FUNC(p)
  105. #endif
  106.  
  107. /*
  108.  * Raw object memory interface
  109.  * ===========================
  110.  */
  111.  
  112. /* The use of this API should be avoided, unless a builtin object
  113.    constructor inlines PyObject_{New, NewVar}, either because the
  114.    latter functions cannot allocate the exact amount of needed memory,
  115.    either for speed. This situation is exceptional, but occurs for
  116.    some object constructors (PyBuffer_New, PyList_New...).  Inlining
  117.    PyObject_{New, NewVar} for objects that are supposed to belong to
  118.    the Python heap is discouraged. If you really have to, make sure
  119.    the object is initialized with PyObject_{Init, InitVar}. Do *not*
  120.    inline PyObject_{Init, InitVar} for user-extension types or you
  121.    might seriously interfere with Python's memory management. */
  122.  
  123. /* Functions */
  124.  
  125. /* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
  126.    need to be sure that you are using the same object memory allocator
  127.    as Python. These wrappers *do not* make sure that allocating 0
  128.    bytes returns a non-NULL pointer. Returned pointers must be checked
  129.    for NULL explicitly; no action is performed on failure. */
  130. extern DL_IMPORT(ANY *) PyObject_Malloc Py_PROTO((size_t));
  131. extern DL_IMPORT(ANY *) PyObject_Realloc Py_PROTO((ANY *, size_t));
  132. extern DL_IMPORT(void) PyObject_Free Py_PROTO((ANY *));
  133.  
  134. /* Macros */
  135. #define PyObject_MALLOC(n)           PyCore_OBJECT_MALLOC(n)
  136. #define PyObject_REALLOC(op, n)      PyCore_OBJECT_REALLOC((ANY *)(op), (n))
  137. #define PyObject_FREE(op)            PyCore_OBJECT_FREE((ANY *)(op))
  138.  
  139. /*
  140.  * Generic object allocator interface
  141.  * ==================================
  142.  */
  143.  
  144. /* Functions */
  145. extern DL_IMPORT(PyObject *) PyObject_Init Py_PROTO((PyObject *, PyTypeObject *));
  146. extern DL_IMPORT(PyVarObject *) PyObject_InitVar Py_PROTO((PyVarObject *, PyTypeObject *, int));
  147. extern DL_IMPORT(PyObject *) _PyObject_New Py_PROTO((PyTypeObject *));
  148. extern DL_IMPORT(PyVarObject *) _PyObject_NewVar Py_PROTO((PyTypeObject *, int));
  149. extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *));
  150.  
  151. #define PyObject_New(type, typeobj) \
  152.         ( (type *) _PyObject_New(typeobj) )
  153. #define PyObject_NewVar(type, typeobj, n) \
  154.         ( (type *) _PyObject_NewVar((typeobj), (n)) )
  155. #define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
  156.  
  157. /* Macros trading binary compatibility for speed. See also mymalloc.h.
  158.    Note that these macros expect non-NULL object pointers.*/
  159. #define PyObject_INIT(op, typeobj) \
  160.     ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
  161. #define PyObject_INIT_VAR(op, typeobj, size) \
  162.     ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
  163.  
  164. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  165. #define _PyObject_VAR_SIZE(typeobj, n) \
  166.     ( (typeobj)->tp_basicsize + (n) * (typeobj)->tp_itemsize )
  167.  
  168. #define PyObject_NEW(type, typeobj) \
  169. ( (type *) PyObject_Init( \
  170.     (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
  171. #define PyObject_NEW_VAR(type, typeobj, n) \
  172. ( (type *) PyObject_InitVar( \
  173.     (PyVarObject *) PyObject_MALLOC( _PyObject_VAR_SIZE((typeobj),(n)) ),\
  174.     (typeobj), (n)) )
  175. #define PyObject_DEL(op) PyObject_FREE(op)
  176.  
  177. /* This example code implements an object constructor with a custom
  178.    allocator, where PyObject_New is inlined, and shows the important
  179.    distinction between two steps (at least):
  180.        1) the actual allocation of the object storage;
  181.        2) the initialization of the Python specific fields
  182.           in this storage with PyObject_{Init, InitVar}.
  183.  
  184.    PyObject *
  185.    YourObject_New(...)
  186.    {
  187.        PyObject *op;
  188.  
  189.        op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  190.        if (op == NULL)
  191.            return PyErr_NoMemory();
  192.  
  193.        op = PyObject_Init(op, &YourTypeStruct);
  194.        if (op == NULL)
  195.            return NULL;
  196.  
  197.        op->ob_field = value;
  198.        ...
  199.        return op;
  200.    }
  201.  
  202.    Note that in C++, the use of the new operator usually implies that
  203.    the 1st step is performed automatically for you, so in a C++ class
  204.    constructor you would start directly with PyObject_Init/InitVar. */
  205.  
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif /* !Py_OBJIMPL_H */
  210.